package com.heartbeat.upyun.impl;

import java.io.IOException;
import java.util.List;

import com.heartbeat.domain.Content;
import com.heartbeat.domain.Page;
import com.heartbeat.spider.ContentDao;
import com.heartbeat.spider.ContentService;
import com.heartbeat.spider.PageDao;
import com.heartbeat.spider.impl.ContentDaoImpl;
import com.heartbeat.spider.impl.ContentServiceImpl;
import com.heartbeat.spider.impl.PageDaoImpl;
import com.heartbeat.upyun.PicBucket;
import com.heartbeat.upyun.PicUploadService;

public class PicUploadServiceImpl implements PicUploadService {

	private ContentDao contentDao = new ContentDaoImpl();
	
	@Override
	public boolean uploadImg(Content content) {
		// TODO Auto-generated method stub
		String dirRoot = "/" + content.getSiteId()+"/";
		//String dirRoot = "/";
		try {
			boolean uploadListPicSucced = PicBucket.fixBoth(content.getPath(), "200x300", dirRoot);
			boolean uploadDetailPicSucced = PicBucket.fixWidth(content.getPath(), "320x480", dirRoot);
			boolean uploadBigPicSucced = PicBucket.fixWidth(content.getPath(), "480x800", dirRoot);
			
			if(uploadListPicSucced&&uploadDetailPicSucced&&uploadBigPicSucced){
				content.setHasUploaded(true);
				contentDao.updateContent(content);
			}
			
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	public static void main(String args[]){
		/*PicUploadService picUploadService = new PicUploadServiceImpl();
		ContentDao contentDao = new ContentDaoImpl();
		Content content  = contentDao.getContentById(10);
		picUploadService.uploadImg(content);*/
		
		ContentDao contentDao = new ContentDaoImpl();
		PicUploadService picUploadService = new PicUploadServiceImpl();
		for(int i=0;i<10000; i++){
			List<Content> contentList = contentDao.queryToUploadImgContentList(0, 100);
			
			//ContentService contentService = new ContentServiceImpl();
			
			for(Content content : contentList){
				picUploadService.uploadImg(content);
			}
			
		}
	}
}
